//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DotNetNuke.UI.WebControls
{
	[ToolboxData("<{0}:DNNRichTextEditControl runat=server></{0}:DNNRichTextEditControl>")]
	public class DNNRichTextEditControl : TextEditControl
	{
		private DotNetNuke.Modules.HTMLEditorProvider.HtmlEditorProvider RichTextEditor;
		public DNNRichTextEditControl()
		{
		}
		protected override void CreateChildControls()
		{
			if (this.EditMode == PropertyEditorMode.Edit) {
				RichTextEditor = DotNetNuke.Modules.HTMLEditorProvider.HtmlEditorProvider.Instance();
				RichTextEditor.ControlID = this.ID + "edit";
				RichTextEditor.Initialize();
				RichTextEditor.Height = this.ControlStyle.Height;
				RichTextEditor.Width = this.ControlStyle.Width;
				if (RichTextEditor.Height.IsEmpty) {
					RichTextEditor.Height = new Unit(300);
				}
				if (RichTextEditor.Width.IsEmpty) {
					RichTextEditor.Width = new Unit(450);
				}
				Controls.Clear();
				Controls.Add(RichTextEditor.HtmlEditorControl);
			}
			base.CreateChildControls();
		}
		public override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
		{
			bool dataChanged = false;
			string presentValue = StringValue;
			string postedValue = RichTextEditor.Text;
			if (!presentValue.Equals(postedValue)) {
				Value = postedValue;
				dataChanged = true;
			}
			return dataChanged;
		}
		protected override void OnDataChanged(EventArgs e)
		{
			string strValue = Convert.ToString(Value);
			string strOldValue = Convert.ToString(OldValue);
			PropertyEditorEventArgs args = new PropertyEditorEventArgs(Name);
			args.Value = this.Page.Server.HtmlEncode(strValue);
			args.OldValue = this.Page.Server.HtmlEncode(strOldValue);
			args.StringValue = this.Page.Server.HtmlEncode(StringValue);
			base.OnValueChanged(args);
		}
		protected override void OnInit(System.EventArgs e)
		{
			this.EnsureChildControls();
			base.OnInit(e);
		}
		protected override void OnPreRender(System.EventArgs e)
		{
			base.OnPreRender(e);
			if (this.EditMode == PropertyEditorMode.Edit) {
				RichTextEditor.Text = this.Page.Server.HtmlDecode(Convert.ToString(this.Value));
			}
			if (Page != null && this.EditMode == PropertyEditorMode.Edit) {
				this.Page.RegisterRequiresPostBack(this);
			}
		}
		protected override void RenderEditMode(System.Web.UI.HtmlTextWriter writer)
		{
			this.RenderChildren(writer);
		}
		protected override void RenderViewMode(System.Web.UI.HtmlTextWriter writer)
		{
			string propValue = this.Page.Server.HtmlDecode(Convert.ToString(this.Value));
			ControlStyle.AddAttributesToRender(writer);
			writer.RenderBeginTag(HtmlTextWriterTag.Span);
			writer.Write(propValue);
			writer.RenderEndTag();
		}
	}
}
